package com.zdkj.subway.rca.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.rca.enums.SwCkClearRuleConstans;
import com.zdkj.subway.rca.service.SwCkClearEmergencyReturnticketService;
import com.zdkj.subway.rca.service.SwCkClearRulesService;
import com.zdkj.subway.rca.util.MyDateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 清分及划账-清分及划账记录-应急纸票与退款清分 
 *
 * @author stw
 * @date 2021-01-01 18:02:59
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckclearemergencyreturnticket" )
@Api(value = "swckclearemergencyreturnticket", tags = "清分及划账-清分及划账记录-应急纸票与退款清分")
public class SwCkClearEmergencyReturnticketController {
	
	@Autowired
    private final SwCkClearEmergencyReturnticketService swCkClearEmergencyReturnticketService;
    @Autowired
    private final SwCkClearRulesService swCkClearRulesService;

    /**
     * 运营商下拉列表
     * @return
     */
    @ApiOperation(value = "运营商下拉列表", notes = "运营商下拉列表")
    @GetMapping("/listoperators" )
    public R listoperators() {
        return R.ok(swCkClearRulesService.getServiceMap());
    }
    /**
     * 分页查询
     * @param
     * @return
     */
    @ApiOperation(value = "分页查询,startDate 格式：yyyy-MM-dd, endDate 格式： yyyy-MM-dd", notes = "分页查询")
    @GetMapping("/list" )
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "开始日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "endDate", value = "结束日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "operators", value = "运营商编码", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "lineCode", value = "线路编码", dataTypeClass = String.class),
            }
    )
    public R getSwCkClearEmergencyReturnticketList(String startDate, String endDate,String lineCode, String operators) {
        if(StringUtils.isEmpty(startDate) ||  StringUtils.isEmpty(endDate)){
            return R.failed("开始日期和结束日期不能为空");
        }
        return R.ok(swCkClearEmergencyReturnticketService.listSwCkClearEmergencyReturnticket(startDate,endDate,lineCode,operators));
    }

    /**
     * 清分及划账-清分及划账记录-应急纸票与退款清分-系统自动清分
     * @return R
     */
    @ApiOperation(value = "清分及划账-清分及划账记录-应急纸票与退款清分-系统自动清分，点击“系统自动清分”按钮弹出窗口选择时间段(二者必须选)", notes = "清分及划账-清分及划账记录-系统自动清分，点击“系统自动清分”按钮弹出窗口选择时间段(二者必须选)")
    @SysLog("清分及划账-清分及划账记录-应急纸票与退款清分-系统自动清分" )
    @GetMapping({"/autoClean"})
    @PreAuthorize("@pms.hasPermission('sw_ck_clear_other_account_autoClean')" )
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "日期 格式yyyy-MM", dataTypeClass = String.class, required = true)                   
            }
    )
    public R autoClean(String startDate) {
        try {
            if (StringUtils.isEmpty(startDate)){
                return R.failed("请选择清分日期");
            }
            int iYear = Integer.parseInt(startDate.split("-")[0]);
            int iMonth = Integer.parseInt(startDate.split("-")[1]);
            String realStartDate = MyDateUtil.getFirstDayOfMonth(iYear, iMonth);
            String realEndDate = MyDateUtil.getLastDayOfMonth(iYear, iMonth);
            Map<String,String> mapLineClearRule = swCkClearRulesService.selectRulesDetailsMapByDate(realStartDate, realEndDate) ;
            if(mapLineClearRule.size()== 0){
                return R.failed("日期范围内没有清分规则！");
            }
            String ruleKeyDaily = SwCkClearRuleConstans.CLEAR_RULE_EMERGENCY_BIGSTREAM+","+"1"+","+SwCkClearRuleConstans.SERVICE_ID_XADT+",1";
            String ruleKeySunivenir = SwCkClearRuleConstans.CLEAR_RULE_RETURN_NORMAL+","+"1"+","+SwCkClearRuleConstans.SERVICE_ID_XADT+",1";
            if(mapLineClearRule.size()> 0 && (ObjectUtil.isEmpty(mapLineClearRule.get(ruleKeyDaily)))){
                return R.failed("日期范围内没有关于应急纸票或者正常情况退票等相关清分规则！");
            }
            return R.ok(swCkClearEmergencyReturnticketService.generateSwCkClearEmergencyReturnTicket(realStartDate, realEndDate));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed();
        }

    }
    /**
     * 导出
     * @param
     * @return
     */
    @ApiOperation(value = "导出", notes = "导出")
    @GetMapping({"/export"})
    @SysLog("应急纸票和退换票清分结果-导出")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "开始日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "endDate", value = "结束日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "operators", value = "运营商编码", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "lineCode", value = "线路编码", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "baseCode", value = "0代表应急纸票和退票清分报表 1代表人工协议清分对账报表退换票 9代表人工协议清分对账报表（9号线） 54代表人工协议清分对账报表（14号线）", dataTypeClass = String.class),
            }
    )
    public void export(String startDate,String endDate,String operators, String lineCode, String baseCode,HttpServletResponse response) {
        if(StringUtils.isEmpty(startDate) ||  StringUtils.isEmpty(endDate)){
            return;
        }
        swCkClearEmergencyReturnticketService.export(startDate,endDate,operators,lineCode,baseCode,response);

    }

    /**
     * 清分及划账-应急纸票与退款清分-重新清分  TODO 待定
     * @return R
     */
    @ApiOperation(value = "清分及划账-清分及划账记录-应急纸票与退款清分-重新清分，必须选择左边复选框，可多选", notes = "清分及划账-清分及划账记录-应急纸票与退款清分-重新清分，必须选择左边复选框，可多选")
    @SysLog("清分及划账-清分及划账记录-应急纸票与退款清分-重新清分" )
    @GetMapping({"/reClean"})
    @PreAuthorize("@pms.hasPermission('sw_ck_clear_other_account_reClean')" )
    public R reClean(@RequestParam(value = "ids") List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()){
                return R.failed();
            }
           return R.ok(swCkClearEmergencyReturnticketService.reGenerateSwCkClearEmergencyReturnTicket(ids));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed();
        }

    }

}
